import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:todo_app/components/Overlay/main.dart';
import 'package:todo_app/components/TextExt.dart';
import 'package:todo_app/constants/DColor.dart';
import 'package:todo_app/controllers/UserController.dart';
import 'package:todo_app/controllers/definitions/UserModel.dart';
import 'package:todo_app/layout/CustomCard.dart';
import 'package:todo_app/layout/Gaps.dart';
import 'package:todo_app/layout/Header.dart';

class PersonQrcodePage extends HookWidget {
  const PersonQrcodePage({ required this.userId, Key? key}) : super(key: key);
  final String userId;

  @override
  Widget build(BuildContext context) {
    var user = useState<UserModel>(UserModel());

    useEffect(() {
      UserController.userInfoUsingGet(userId).then((res)=> {
        if(res.status == 'success') {
          user.value = res.data!
        }
      });
      return () {};
    }, []);

    var name = user.value.username;
    return Scaffold(
      appBar: const Header(title: "用户二维码", brightness: Brightness.light,),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          CustomCard(
            padding: EdgeInsets.all(80.dp),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                QrImageView(
                  data: 'todoapp://user/$userId',
                  version: QrVersions.auto,
                  size: 200.dp,
                ),
                Gaps.vGap32,
                TextExt.desc("使用“我的”页面“扫一扫”功能可查看主页", fontSize: 12.dp),
                Gaps.vGap8,
                TextExt.desc("用户名${name ?? ""}", fontSize: 12.dp),
                Gaps.vGap8,
                GestureDetector(
                  onTap: () {
                    Clipboard.setData(ClipboardData(text: name ?? ""));
                    showOverlayToast("复制用户名成功");
                  },
                  child: TextExt.desc("复制用户名", fontSize: 12.dp, style: const TextStyle(color: DColor.primary))
                ),
              ],
            )
          )
        ],  
      )
    );
  }
}